package com.example.screw;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.ArrayList;

public class GenerateDBDoc {

    public static void main(String[] args) {
// 开发环境
//        String dbName = "prk";
//        String systemName = "prk系统";
//        String jdbcUrl = String.join("", "jdbc:mysql://172.16.197.89:3307/", dbName, "?useUnicode=true&characterEncoding=utf-8");
//        String userName = "root";
//        String pwd = "lgb";
        String dbName = "customer";
        String systemName = "客户系统";
        String jdbcUrl = String.join("", "jdbc:mysql://10.70.6.15:3306/", dbName, "?useUnicode=true&characterEncoding=utf-8");
        String userName = "app_all_d";
        String pwd = "mHCBntg53e4qnfWCBoLGkCxe7jC1cy";
        String outputFolder = "d:/dbdoc";
        String docVersion = "1.0.3";
        String fileName = String.join("-", "DB", dbName, docVersion);
        String docDescription = systemName + "数据库文档";

        documentGeneration(jdbcUrl, userName, pwd, outputFolder, fileName, docVersion, docDescription);

        // 测试D emp_mz
        /*hikariConfig.setJdbcUrl("jdbc:mysql://10.70.6.15:3306/emp_mz?useUnicode=true&characterEncoding=utf-8");
        hikariConfig.setUsername("app_all_d");
        hikariConfig.setPassword("mHCBntg53e4qnfWCBoLGkCxe7jC1cy");*/
        // 测试D customer
        /*
        "jdbc:mysql://10.70.6.15:3306/customer?useUnicode=true&characterEncoding=utf-8","app_all_d", "mHCBntg53e4qnfWCBoLGkCxe7jC1cy", "DB-emp", "1.0.0", "数据库设计文档生成"
         */
        // 开发 gateway
        /*hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/gateway?useUnicode=true&characterEncoding=utf-8");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("123456");*/
    }

    /**
     * 文档生成
     */
    static void documentGeneration(String jdbcUrl, String username, String pwd, String outputFolder, String fileName, String docVersion, String docDescription) {
        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(pwd);

        //设置可以获取tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成文件路径
                .fileOutputDir(outputFolder)
                //打开目录
                .openOutputDir(true)
                //文件类型
                .fileType(EngineFileType.HTML)
                //生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //自定义文件名称
                .fileName(fileName)
                .build();

        //忽略表
        ArrayList<String> ignoreTableName = new ArrayList<>();
        ignoreTableName.add("test_user");
        ignoreTableName.add("test_group");
        ignoreTableName.add("dictionary1");
        ignoreTableName.add("dictionary_zd3");
        ignoreTableName.add("region_hs");
        ignoreTableName.add("share_order");
        ignoreTableName.add("login_info");
        ignoreTableName.add("qa_change_hs");
        //忽略表前缀
        ArrayList<String> ignorePrefix = new ArrayList<>();
        ignorePrefix.add("test");
        ignorePrefix.add("test_");
        ignorePrefix.add("backup_");
        ignorePrefix.add("tmp_");
        ignorePrefix.add("temp_");
        //忽略表后缀
        ArrayList<String> ignoreSuffix = new ArrayList<>();
        ignoreSuffix.add("_test");
        ignoreSuffix.add("_copy");
        ignoreSuffix.add("_copy1");
        ignoreSuffix.add("_copy2");
        ignoreSuffix.add("_copy3");
        ignoreSuffix.add("_copy4");
        ignoreSuffix.add("_copy5");
        ignoreSuffix.add("_tmp");
        ignoreSuffix.add("_temp");
        ignoreSuffix.add("_bak");
        ignoreSuffix.add("_back");
        ignoreSuffix.add("_text1");
        ignoreSuffix.add("_pro");
        ignoreSuffix.add("_rzmove");
        ignoreSuffix.add("_a");
        ignoreSuffix.add("_jt");
        ignoreSuffix.add("_mz");
        ignoreSuffix.add("_19.1.8");
        ignoreSuffix.add("_1269");
        ignoreSuffix.add("_2020526");
        ignoreSuffix.add("_210125");
        ignoreSuffix.add("_20211202");
        ignoreSuffix.add("_20190918");
        ignoreSuffix.add("_20200924");
        ignoreSuffix.add("_20200606");
        ignoreSuffix.add("_20190927");
        ignoreSuffix.add("_252");
        ignoreSuffix.add("_255");
        ignoreSuffix.add("_629");
        ignoreSuffix.add("_1");
        ignoreSuffix.add("_2");
        ignoreSuffix.add("_3");
        ignoreSuffix.add("_12");
        ignoreSuffix.add("_1312");
        ignoreSuffix.add("_100");
        ignoreSuffix.add("_20200723");
        ignoreSuffix.add("_20201214");
        ignoreSuffix.add("_20200515");
        ignoreSuffix.add("_20201117");
        ignoreSuffix.add("_20200908");
        ignoreSuffix.add("_20190308");
        ProcessConfig processConfig = ProcessConfig.builder()
                //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时，将生成指定表，其余表不生成、并跳过忽略表配置
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
        //配置
        Configuration config = Configuration.builder()
                //版本
                .version(docVersion)
                //描述
                .description(docDescription)
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                //生成配置
                .produceConfig(processConfig)
                .build();
        //执行生成
        new DocumentationExecute(config).execute();
    }
}
